

var role = -1; // 选择的角色

function checkPhone(phone) {
    if(phone.search(/^1[3458]{1}[0-9]{9}$/) == -1)
        return false;
    else
        return true;
}



var login = {
    chooseRole: function() {
        $('.login-role li').on('click', function() {
            var $this = $(this);
            $this.addClass('is-active').siblings().removeClass('is-active');
            role = $this.attr('data-id');
        })
    },
    check: function() {
        $('[data-role="phone"]').on('blur', function() {
            var value = $(this).val();
            if(value == '') {
                $(this).next('.tips').html('用户名不能为空');
            } else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="password"]').on('blur', function() {
            var value = $(this).val();
            if(value == '') {
                $(this).next('.tips').html('密码不能为空');
            } else {
                $(this).next('.tips').html('');
            }
        })
    },
    submit: function() {

        $('[data-role="submit"]').on('click', function() {
            if(role == -1) {
                iosOverlay({
                    text: '请先选择登录的角色',
                    duration: 1000
                });
            } else {
                var phone = $('[data-role="phone"]').val();
                var password = $('[data-role="password"]').val();

                if(phone == '') {
                    $('[data-role="phone"]').next('.tips').html('用户名不能为空');
                } else {
                    $('[data-role="phone"]').next('.tips').html('');
                }

                if(password == '') {
                    $('[data-role="password"]').next('.tips').html('密码不能为空');
                } else {
                    $('[data-role="password"]').next('.tips').html('');
                }

                if(phone != '' && password != '') {
                    $.ajax({
                        url: window.domain + window.requestUrl.loginUrl + role,
                        type: 'post',
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        data: {
                            principal: phone,
                            pass: $('[data-role="password"]').val()
                        },
                        success: function(res) {
                            if(res.status) {
                                if(res.data.user.type == 1) {
                                    var link = window.getCookie('go_back_link');
                                    window.delCookie('go_back_link');
                                    window.location.href = link ? link : './weixin-show.html';
                                } else {
                                    window.location.href = './index.html';
                                }
                            } else {
                                iosOverlay({
                                    text: res.msg,
                                    duration: 1000
                                });
                            }
                        },
                        error: function(e) {
                        }
                    })
                }

            }
        })
    },
    init: function() {
        this.submit();
        this.chooseRole();
        this.check();
    }
}

login.init();
